package com.meichis.yndms.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.LatLngBounds;
import com.amap.api.maps2d.model.PolylineOptions;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.VstTravelAdapter;
import com.meichis.yndms.common.MCode;
import com.meichis.yndms.databinding.ActivityVstTravelBinding;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.VstTravelVM;

import java.util.ArrayList;
import java.util.List;

public class VstTravelActivity extends BaseActivity {

    private ActivityVstTravelBinding binding;
    private VstTravelVM vm;


    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_vst_travel);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(VstTravelVM.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("业务行程轨迹");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());
        binding.navigation.funBtn.setText("切换");
        binding.navigation.funBtn.setOnClickListener(v -> {
            if (binding.rc.getVisibility() == View.GONE) {
                binding.rc.setVisibility(View.VISIBLE);
                binding.map.setVisibility(View.GONE);
            } else {
                binding.map.setVisibility(View.VISIBLE);
                binding.rc.setVisibility(View.GONE);
            }
        });
        addObserver();
        vm.setStaffID(getIntent().getStringExtra(MCode.STAFF));
        vm.GetYWRBFGJ(0);

        binding.map.getMap().setOnMapClickListener((LatLng latLng) -> {
            //点击其它地方隐藏infoWindow
            if (vm.curShowWindowMarker != null) {
                vm.curShowWindowMarker.hideInfoWindow();
            }
        });
        binding.map.getMap().setOnMarkerClickListener(marker -> {
            if (vm.curShowWindowMarker != null) {
                vm.curShowWindowMarker.hideInfoWindow();
            }
            vm.curShowWindowMarker = marker;
            return false;
        });
    }


    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));
        vm.getVstTravelList().observe(this, vstTravels -> {

            //用于连线
            List<LatLng> latLngs = new ArrayList<>();
            //用于显示全部点
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            LatLngBounds bounds = null;

            for (int i = 0; i < vstTravels.size(); i++) {
                View view = View.inflate(getActivity(), R.layout.layout_travel_location, null);
                TextView tvName = view.findViewById(R.id.tv_name);
                tvName.setText(String.valueOf(i + 1));
                Bitmap bitmap = convertViewToBitmap(view);
                binding.map.addMarker(vstTravels.get(i).getLat(), vstTravels.get(i).getLng(), bitmap,
                        vstTravels.get(i).getTerminalName(), "拜访时间：" + vstTravels.get(i).getIDate());
                LatLng latLng = new LatLng(vstTravels.get(i).getLat(), vstTravels.get(i).getLng());
                latLngs.add(latLng);
                builder.include(latLng);//把你所有的坐标点放进去
            }
            //连线
            binding.map.getMap().addPolyline(new PolylineOptions().
                    addAll(latLngs).width(6).color(getResources().getColor(R.color.theme)));
            //显示所有点
            bounds = builder.build();
            binding.map.getMap().moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 36));

            VstTravelAdapter adapter = new VstTravelAdapter(this, R.layout.item_time_line, vstTravels);
            binding.rc.setLayoutManager(new LinearLayoutManager(this));
            binding.rc.setAdapter(adapter);
            binding.rc.setItemViewCacheSize(vstTravels.size());
        });
    }


    public class ClickEvent {

        public void onSelectToDay() {
            binding.tvYesterday.setTextColor(getResources().getColor(R.color.txt));
            binding.tvYesterday.setTextSize(12);
            binding.vYesterday.setVisibility(View.GONE);

            binding.tvToday.setTextColor(getResources().getColor(R.color.theme));
            binding.tvToday.setTextSize(16);
            binding.vToday.setVisibility(View.VISIBLE);
            vm.GetYWRBFGJ(0);
            binding.map.getMap().clear();
        }

        public void onSelectYesterday() {
            binding.tvToday.setTextColor(getResources().getColor(R.color.txt));
            binding.tvToday.setTextSize(12);
            binding.vToday.setVisibility(View.GONE);

            binding.tvYesterday.setTextColor(getResources().getColor(R.color.theme));
            binding.tvYesterday.setTextSize(16);
            binding.vYesterday.setVisibility(View.VISIBLE);
            vm.GetYWRBFGJ(1);
            binding.map.getMap().clear();
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding.map.setUpMap(savedInstanceState);
        binding.map.getMap().setMyLocationEnabled(false);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        binding.map.onSaveInstanceState(outState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (binding.map != null) {
            //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
            binding.map.onResume();
        }
    }

    //view 转bitmap

    public static Bitmap convertViewToBitmap(View view) {

        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        view.buildDrawingCache();

        Bitmap bitmap = view.getDrawingCache();

        return bitmap;

    }


    @Override
    protected void onPause() {
        if (binding.map != null) {
            //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
            binding.map.onPause();
        }
        super.onPause();

    }

    @Override
    protected void onDestroy() {
        if (binding.map != null) {
            //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
            binding.map.onDestroy();
        }
        super.onDestroy();

    }
}
